(function($, doc, phone) {
	mui.init();
	mui.plusReady(function() {
		var settings = app.getSettings();
		var state = app.getState();
		var uid = state.user_id;
		//doc.querySelector("#defaultcontactTab").classList.remove('mui-active');
		//doc.querySelector("#defaultcontactTab").classList.add('mui-active');
		//title.innerHTML = doc.querySelector("#defaultcontactTab").querySelector('.mui-tab-label').innerHTML;
		/*
		document.addEventListener('swiperight', function() {
			//location.href = 'chat.html';
			plus.webview.hide('contact.html');
			plus.webview.show('room.html', "slide-in-left", 300);

		});
		document.addEventListener('swipeleft', function() {
			//location.href = 'find.html';
			plus.webview.hide('contact.html');
			plus.webview.show('me.html', "slide-in-left", 300);
		});*/
		//var contactdata;
		//var cdata = localStorage.getItem('contactlist' + uid);
		//var cdatalen = cdata.length;
		var list = document.getElementById('list');
		var header = document.querySelector('header');
		var oft = plus.display.resolutionHeight;
		var bft = document.body.offsetHeight;
		if(oft < bft) {
			oft = bft;
		}
		//if(!oft || oft < 300){
		//	oft = 520;
		//window.reload();
		//}
		//calc hieght
		list.style.height = oft - 110 + 'px'; //gz
		document.getElementById('scroll').style.height = oft - 110 + 'px';
		//list.style.height = document.body.offsetHeight + 'px';
		//create
		//window.indexedList = new mui.IndexedList(list); //gz
		//var cdata = null;
		//console.log(cdata);
		function renderlist() {
			document.querySelector("#list-area").innerHTML = template('list-template', {
				"flist": phones
			});
		}
		var phones = sysphone.getPhonePin();
		if(phones.length > 0) {
			renderlist();
		}else{
			setTimeout(function() {
				var phones = sysphone.getPhonePin();
				renderlist();
			}, 1000);
		}

		mui(".friend-searchbox").on('input', '#searchInput', function() {
			var val = this.value;
			//console.log(val);
			if(val == '') {
				document.querySelector("#list-area").innerHTML = template('list-template', {
					"flist": phones
				});
			} else {
				var newlist = {};
				for(var zimu in phones) {
					var plist = phones[zimu];
					for(var p in plist) {
						var item = plist[p];
						if(item['name'].indexOf(val) !== -1 || item['phone'].indexOf(val) !== -1) {
							if(!newlist[zimu]) {
								newlist[zimu] = {};
							}
							newlist[zimu][p] = item;
						}
					}
				}
				document.querySelector("#list-area").innerHTML = template('list-template', {
					"flist": newlist
				});;
				//console.log('5');
			}
		});
		//initheight();
		mui("#list-area").on("tap", "a", function() {
			var fid = this.getAttribute("fid");
			var head = this.getAttribute("head");
			var fname = this.getAttribute("title");
			var phone = this.getAttribute("phone");
			var targets = '/pages/user/other_detail.html';
			mui.openWindow({
				url: targets,
				id: targets,
				extras: {
					fid: fid,
					head: head,
					fname: fname,
					phone: phone
				}
			});

		});
		//console.log(JSON.stringify(phones));  
		/*
		if(cdata !== null) {
			contactdata = JSON.parse(cdata);
			renderlist(contactdata);
		} else {
			setData();
		}
		setTimeout(function() {
			setData();
		}, 300);

		function setData() {
			var getUrl = apiurl + "user/friendslist/?user_id=" + uid;
			mui.getJSON(getUrl, function(res) {
				//console.log(JSON.stringify(res.list));
				if(res.status > 0) {
					if(cdata === null) {
						renderlist(res.list);
					}
					localStorage.setItem('contactlist' + uid, JSON.stringify(res.list));
				}
			});
		}

		function renderlist(data) {
			document.querySelector("#list-area").innerHTML = template('list-template', {
				"flist": data
			});
			//initheight();
			mui("#list-area").on("tap", "a", function() {
				var fid = this.getAttribute("fid");
				var head = this.getAttribute("head");
				var fname = this.getAttribute("title");
				var targets = '/pages/user/other_detail.html';
				mui.openWindow({
					url: targets,
					id: targets,
					extras: {
						fid: fid,
						head: head,
						fname: fname
					}
				});

			});
		}*/
		mui("#tophrefs").on("tap", "a", function() {
			var targets = this.getAttribute("href");
			//location.href = targets;
			mui.openWindow({
				url: targets,
				id: targets
			})

		});
		/*
		var mainid = localStorage.getItem('mainpage');
		var mainWin = plus.webview.getWebviewById(mainid);
		doc.addEventListener('swipeleft', function(event) {
			var angle = event.detail.angle;
			angle = Math.abs(angle);
			if(angle > 165 && angle < 195) {
				mui.fire(mainWin, 'swipe_event', {
					target: 'find.html'
				});
			}
		});
		doc.addEventListener('swiperight', function(event) {
			if(Math.abs(event.detail.angle) < 30) {
				mui.fire(mainWin, 'swipe_event', {
					target: 'chat.html'
				});
			}
		});*/
		
		/*
		*/

	});
}(mui, document, window.phone));